//
//  ReceiveTableViewCell.m
//  leo
//
//  Created by 常艳 on 15/10/4.
//  Copyright (c) 2015年 mdx. All rights reserved.
//

#import "ReceiveTableViewCell.h"
#import "AppDelegate.h"
#import "DataManager.h"
@implementation ReceiveTableViewCell

- (void)awakeFromNib {
    // Initialization code
    [self.headImg.layer setMasksToBounds:YES];
    self.headImg.layer.cornerRadius=self.headImg.bounds.size.width/2;
    self.headImg.clipsToBounds=YES;
    self.is_play=NO;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(startPlay:)];
    tap.delegate=self;
    [self.voiceImg addGestureRecognizer:tap];
    DataManager *dataManager=[[DataManager alloc] init];
    if(dataManager==NULL)
        return;
    if(self.isRead==1)
    {
        self.unreadImg.hidden=YES;
    }

}
-(void)initUI
{
    AppDelegate *mydelegate=[[UIApplication sharedApplication] delegate];
    if(mydelegate!=NULL&&mydelegate.receiveCell!=NULL)
    {
        if(mydelegate.receiveCell==self)
        {
            self.voiceBtn.imageView.animationImages=[NSArray arrayWithObjects:
                                                     [UIImage imageNamed:@"voice_b_anim1@2x.png"],
                                                     [UIImage imageNamed:@"voice_b_anim2@2x.png"],
                                                     [UIImage imageNamed:@"voice_b_anim3@2x.png"],                                           nil];
            [self.voiceBtn.imageView setAnimationDuration:2.0f];
            [self.voiceBtn.imageView startAnimating];
        }
    }
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (IBAction)startPlay:(UITapGestureRecognizer *)sender
{
    [self playAction];
    self.unreadImg.hidden=YES;
}
- (IBAction)playRecord:(id)sender
{

     self.unreadImg.hidden=YES;
    [self playAction];
   

}
-(void)playAction
{
    if(self.isRead==0)
    {
        DataManager *dataManager=[[DataManager alloc] init];
        if(dataManager==NULL)
            return;
        BOOL result=[dataManager setChatRead:self.chatId];
        if(result)
            self.isRead=1;
    }
    AppDelegate *mydelegate=[[UIApplication sharedApplication] delegate];
    if(mydelegate.receiveCell!=NULL)
    {
        if(mydelegate.receiveCell!=self)
        {
            [mydelegate.receiveCell stop];
            mydelegate.receiveCell=NULL;
        }

    }
    if(mydelegate.sendCell!=NULL)
    {
        [mydelegate.sendCell stop];
        mydelegate.sendCell=NULL;
    }
    mydelegate.receiveCell=self;
    if(!self.is_play)
    {
        self.voiceBtn.imageView.animationImages=[NSArray arrayWithObjects:
                                                 [UIImage imageNamed:@"voice_w_anim1@2x.png"],
                                                 [UIImage imageNamed:@"voice_w_anim2@2x.png"],
                                                 [UIImage imageNamed:@"voice_w_anim3@2x.png"],                                           nil];
        [self.voiceBtn.imageView setAnimationDuration:2.0f];
        [self.voiceBtn.imageView startAnimating];
        [self play:self.recordPath];
        self.is_play=YES;
    }
    else
    {
        [self.player stop];
        [self.voiceBtn.imageView stopAnimating];
        self.voiceBtn.imageView.animationImages=NULL;
        self.is_play=NO;
    }
}
-(void)play:(NSString *)path
{
    if(path==NULL)
    {
        NSLog(@"%@,play,path is null.",receive_tag);
        return;
    }
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    path=[documentsDirectory stringByAppendingString:path];
    NSLog(@"%@,play,path is %@.",receive_tag,path);
    //文件管理器
    NSFileManager *fileManager = [NSFileManager defaultManager];
    //判断数据库是否存在
    BOOL find = [fileManager fileExistsAtPath:path];
    if(find)
    {
        AVAudioSession *audioSession=[AVAudioSession sharedInstance];
        [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
        [audioSession setActive:YES error:nil];
        NSError *error=nil;
        self.player=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL URLWithString:path] error:&error];
        if(error!=nil)
        {
            NSLog(@"%@,playRecord,create player error,error is %@.",receive_tag,error);
            return;
        }
        self.player.delegate=self;
        self.player.numberOfLoops=0;
        [self.player setVolume:1.0];
        [self.player prepareToPlay];
        [self.player play];
    }
    else
    {
        NSLog(@"%@,playRecord,the record file is not exist.",receive_tag);
    }
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    [self stop];
    AppDelegate *mydelegate=[[UIApplication sharedApplication] delegate];
    if(mydelegate!=NULL&&mydelegate.receiveCell!=NULL)
    {
        if(mydelegate.receiveCell==self)
            mydelegate.receiveCell=NULL;
    }
}
-(void)stop
{
    if(self.player!=NULL)
    {
        [self.player stop];
        [self.voiceBtn.imageView stopAnimating];
        self.voiceBtn.imageView.animationImages=NULL;
    }
    self.is_play=NO;
}
@end
